{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run spark in yarn client mode"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### yarn ( Apache Hadoop 3.1.2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We run yarn by the following instruction https://hadoop.apache.org/docs/r3.1.2/hadoop-project-dist/hadoop-common/SingleCluster.html\n",
    "\n",
    "Additionally we have to add the following property to etc/hadoop/yarn-site.xml\n",
    "\n",
    "```\n",
    "<property>\n",
    "    <name>yarn.nodemanager.vmem-pmem-ratio</name>\n",
    "    <value>5</value>\n",
    "</property>\n",
    "```\n",
    "\n",
    "or\n",
    "\n",
    "```\n",
    "<property>\n",
    "    <name>yarn.nodemanager.pmem-check-enabled</name>\n",
    "    <value>false</value>\n",
    "</property>\n",
    "\n",
    "<property>\n",
    "    <name>yarn.nodemanager.vmem-check-enabled</name>\n",
    "    <value>false</value>\n",
    "</property>\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### dependencies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%classpath add mvn\n",
    "org.apache.spark spark-repl_2.12 2.4.4\n",
    "org.apache.spark spark-yarn_2.12 2.4.4"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### yarn with beakerx support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%spark --yarn\n",
    "SparkSession.builder()\n",
    ".master(\"yarn\")\n",
    ".appName(\"SparkYarnBeakerxSupport\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scala.math.random\n",
    "val NUM_SAMPLES = 10000000\n",
    "\n",
    "val count2 = spark.sparkContext.parallelize(1 to NUM_SAMPLES).map{i =>\n",
    "  val x = random\n",
    "  val y = random\n",
    "  if (x*x + y*y < 1) 1 else 0\n",
    "}.reduce(_ + _)\n",
    "\n",
    "println(\"Pi is roughly \" + 4.0 * count2 / NUM_SAMPLES)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### yarn without beakerx support\n",
    "additional configuartion:\n",
    " - spark.yarn.jars\n",
    " - spark.repl.class.outputDir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import org.apache.spark.sql.SparkSession\n",
    "val spark = SparkSession.builder().appName(\"SparkYarnNative\")\n",
    ".master(\"yarn\")\n",
    ".config(\"spark.submit.deployMode\", \"client\")\n",
    ".config(\"spark.yarn.jars\", KernelInfo.mvnRepoPath()+\"/*\")\n",
    ".config(\"spark.repl.class.outputDir\",  KernelInfo.outputDir())\n",
    ".getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scala.math.random\n",
    "val NUM_SAMPLES = 10000000\n",
    "\n",
    "val count2 = spark.sparkContext.parallelize(1 to NUM_SAMPLES).map{i =>\n",
    "  val x = random\n",
    "  val y = random\n",
    "  if (x*x + y*y < 1) 1 else 0\n",
    "}.reduce(_ + _)\n",
    "\n",
    "println(\"Pi is roughly \" + 4.0 * count2 / NUM_SAMPLES)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "spark.stop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "",
   "name": "Scala",
   "nbconverter_exporter": "",
   "version": "2.12.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
